package com.hava.contentsale.utils.security;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by yanfa on 2016/11/9.
 */
public class MD5 {

    public final static String hex(String str)
    {
        return DigestUtils.md5Hex(str);
    }

    public final static String get(String str) throws NoSuchAlgorithmException {
        System.out.println("Class MD5 Method get Debug:run");
        MessageDigest md5 = MessageDigest.getInstance("MD5");

        //md5 update string
        md5.reset();
//        md5.update(str.getBytes());
        md5.update(str.getBytes(Charset.forName("UTF8")));
        byte [] result = md5.digest();

        //md5 hexdigest
        return Hex.encodeHex(result).toString();
    }
}
